IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_PageControlSave]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_PageControlSave]
GO
CREATE procedure [dbo].[cms_sp_PageControlSave]
	@PageControlId int,
	@SiteGroupId int = null,
	@SiteId int = null,
	@SystemLanguageId int = null,
	@PageControlContentId int = null,
	@ModuleId int = null,
	@PageControlContent nvarchar(max) = null,
	@PageControlViewValue xml = null,
	@PageControlModelValue xml = null,
	@LastUpdatedBy int,
	@DateLastUpdated datetime,
	@WorkflowId uniqueidentifier = null,
	@IsPublished bit = null,
	@ContentTypeParameter nvarchar(400),
	@PageContentTypeParameter nvarchar(150),
	@PageControlTypeParameter nvarchar(150),
	@PageControlOrder int
as
declare @IsLatestRevision bit
declare @IsLatestCompletedRevision bit
declare @IsDeleted bit
declare @PageId int
declare @TemplateRegionId int
declare @PageControlTypeKey int
declare @NewPageControlId int
if @IsPublished is null
begin
	set @IsPublished = 1
end
set @IsDeleted = 0
set @IsLatestRevision = 1
set @IsLatestCompletedRevision = @IsLatestRevision 
if @WorkflowId is null
begin
	set @IsLatestCompletedRevision = 1
end
select 
	@PageId = PageId,
	@TemplateRegionId = TemplateRegionId,	
	@PageControlTypeKey = PageControlTypeKey
from dbo.PageControls
where PageControlId = @PageControlId
if @PageControlContentId is not null and @PageControlTypeKey = 1 -- page content
begin
	declare @ContentTypeId int
	declare @ContentId int	
	select 
		@ContentTypeId = ContentTypeId 
	from [dbo].[ContentTypes]
	where ContentTypeName = @PageContentTypeParameter
	insert into dbo.Contents
	(
		SiteId,
		SiteGroupId,
		SystemLanguageId,
		ContentTypeId,
		PublicContentId,
		ContentTitle,
		ContentBody,
		CreatedBy,
		DateCreated
	)
	values
	(
		@SiteId,
		@SiteGroupId,
		@SystemLanguageId,
		@ContentTypeId,
		@PageControlContentId,
		'page-content',
		@PageControlContent,
		@LastUpdatedBy,
		@DateLastUpdated
	)
	set @ContentId = SCOPE_IDENTITY()
	update dbo.EntityRevisions
		set IsLatestRevision = 0,
			IsLatestCompletedRevision = 0
	where PublicEntityId = @PageControlContentId and 
		EntityTypeParameter = @ContentTypeParameter
	insert into dbo.EntityRevisions
	(
		EntityId,
		PublicEntityId,
		EntityTypeParameter,
		WorkflowId,
		IsLatestRevision,
		IsLatestCompletedRevision,
		IsDeleted,
		IsPublished
	)
	values
	(
		@ContentId,
		@PageControlContentId,
		@ContentTypeParameter,
		@WorkflowId,
		@IsLatestRevision,
		@IsLatestCompletedRevision,
		@IsDeleted,
		@IsPublished
	)
end
insert into dbo.PageControls
(
	PageId,
	TemplateRegionId,
	ModuleId,
	PageControlContentId,
	PageControlTypeKey,
	PageControlViewValue,
	PageControlModelValue,
	PageControlOrder,
	CreatedBy,
	DateCreated
)
values
(
	@PageId,
	@TemplateRegionId,
	@ModuleId,
	@ContentId,
	@PageControlTypeKey,
	@PageControlViewValue,
	@PageControlModelValue,
	@PageControlOrder,
	@LastUpdatedBy,
	@DateLastUpdated
)
set @NewPageControlId = SCOPE_IDENTITY()
update dbo.EntityRevisions
	set IsLatestRevision = 0,
		IsLatestCompletedRevision = 0
where EntityId = @PageControlId and 
	EntityTypeParameter = @PageControlTypeParameter
insert into dbo.EntityRevisions
(
	EntityId,
	PublicEntityId,
	EntityTypeParameter,
	WorkflowId,
	IsLatestRevision,
	IsLatestCompletedRevision,
	IsDeleted,
	IsPublished
)
values
(
	@NewPageControlId,
	@PageControlId,
	@PageControlTypeParameter,
	@WorkflowId,
	@IsLatestRevision,
	@IsLatestCompletedRevision,
	@IsDeleted,
	@IsPublished
)


GO

